package com.qf.web.servlet;

import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderDetailService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceimpl;
import com.qf.service.impl.CartServiceimpl;
import com.qf.service.impl.OrderDetailServiceimpl;
import com.qf.service.impl.OrderServiceimpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@WebServlet(name = "OrderServlet",value = "/orderservlet")
public class OrderServlet extends BaseServlet {
    //订单预览列表
     public String getOrderView(HttpServletRequest request, HttpServletResponse response){
     //获取购买的商品
     //调用商品业务  findcart
     // 获取收获地址
     //调用用户业务 findaddress
         User user = (User) request.getSession().getAttribute("user");
         System.out.println("走这了");
         if (user==null){
             request.setAttribute("msg","请先登陆才能查看购物车");
             return "/login.jsp";
         }else {
             //
             CartService cartService=new CartServiceimpl();
             try {
                 List<Cartltem> cartltems = cartService.selectUserCartAll(user.getId());
                 AddressService addressService=new AddressServiceimpl();
                 List<Address> addresses = addressService.selectUserAddress(user.getId());
                 request.setAttribute("cartList",cartltems);
                 request.setAttribute("addList",addresses);
                 return "/order.jsp";
             }catch (Exception e){
                 e.printStackTrace();
                 request.setAttribute("msg","信息查询失败");
                 return "/message.jsp";
             }
         }

     }
     //应该是addOrderdatail
     public String addOrder(HttpServletRequest request, HttpServletResponse response){
         User user = (User) request.getSession().getAttribute("user");
//         System.out.println("走这了");
         if (user==null){
             request.setAttribute("msg","请先登陆才能查看购物车");
             return "/login.jsp";
         }else {
             String aid = request.getParameter("aid");
             if (StringUtils.empty(aid)){
                 request.setAttribute("msg","地址获取失败");
                 return "/message.jsp";
             }
             //提交订单那么就相当于添加Order 一条订单信息   那么我门还需要添加一条订单详情    最后我们要删除购物车里面的信息
             //但是一个用户可以同时选购多个不同的商品进行购物，那么购物车里面就存在多个商品，那么就是一个集合
             //但是多个商品都是一块提交，所以我们就要一块来产生一个订单，那么我们就要对集合累加获取money 然后才能给添加给Order
             //还有就是Order中的id其实是订单号，那么我门可以根据每分每秒的方式来生成订单号，然后给予订单信息中的id
             //订单的状态有四种  1 提交未支付   2  已支付，未发货  3  已发货  4  已完成  那么我们这个时候是提交订单 ，状态码为1
             //订单里面的时间   就相当于是某时刻提交订单的  ，new data（）来获取此刻的时间
             //最后把订单的aid序号传进去
             CartService cartService=new CartServiceimpl();
             try {
                 List<Cartltem> cartltems = cartService.selectUserCartAll(user.getId());
                 BigDecimal bigDecimal=new BigDecimal(0);
                 for (Cartltem cartltem:cartltems){
                     bigDecimal=bigDecimal.add(cartltem.getMoney());
                 }
                 Order order=new Order(RandomUtils.createOrderId(),user.getId(),bigDecimal,"1",new Date(),Integer.parseInt(aid));
                 OrderService orderService=new OrderServiceimpl();
                 orderService.addOrder(order);
                 request.setAttribute("order",order);
                 return "/orderSuccess.jsp";
             }catch (Exception e){
                 e.printStackTrace();
                 request.setAttribute("msg","添加失败");
                 return "/message.jsp";
             }
         }

     }
     //测试的servlet转servlet
     public String sss(HttpServletRequest request, HttpServletResponse response){
         System.out.println("sss");
         return "/message.jsp";
     }
     //订单界面
     public String getOrderList(HttpServletRequest request, HttpServletResponse response){
         User user = (User) request.getSession().getAttribute("user");
         if (user==null){
             request.setAttribute("msg","请先登陆再看订单信息好吗");
             return "/message.jsp";
         }else {
            request.setAttribute("loginUser", user);
            OrderService orderService=new OrderServiceimpl();
            List<Order> list=orderService.selectUserOne(user.getId());
            for (Order order:list){
                order.setUser(user);
                AddressService addressService=new AddressServiceimpl();
                Address address = addressService.selectUserAddressOne(order.getAid());
                order.setAddress(address);
            }
            request.setAttribute("orderList",list);
            return "/orderList.jsp";
         }
     }
     //查询详细订单
     public String getOrderDetail(HttpServletRequest request, HttpServletResponse response){
         User user = (User) request.getSession().getAttribute("user");
         if (user==null){
             request.setAttribute("mag","查询详细订单失败");
             return "/message.jsp";
         }else {
             String oid = request.getParameter("oid");
             OrderDetailService orderDetailService=new OrderDetailServiceimpl();
             List<OrderDetail> list=orderDetailService.selectOrderDetailAll(oid);
             OrderDetail orderDetail=orderDetailService.selectOrderDetailOne(oid);
//             OrderService orderService=new OrderServiceimpl();
//             orderServic
             request.setAttribute("order1",orderDetail);
             request.setAttribute("orderDetails",list);
             return "/orderDetail.jsp";
         }
     }
}
